<template>
  <div @click="go" v-if="show">
    <i class="back" v-drag>
        <van-icon class="back-icon" :name="icon" />
    </i>
  </div>
</template>

<script>
export default {
  name: 'back',
  comments: 'back',
  props: {
    direction: {
      type: String,
      default: 'back'
    },
    scroll: {
      type: Boolean,
      default: true
    },
    icon: {
      type: String,
      default: 'wap-home'
    },
    router: {
      type: String,
      default: 'shop'
    }
  },
  data () {
    return {
      show: false
    }
  },
  created () {
    var that = this
    if (this.scroll) {
      window.onscroll = function () {
        var top = document.documentElement.scrollTop || document.body.scrollTop
        if (top < 100) {
          that.show = false
        } else {
          that.show = true
        }
      }
    } else {
      this.show = true
    }
  },
  methods: {
    go () {
      if (this.direction === 'back') {
        this.$router.push({
          name: this.router
        })
      } else {
        (document.documentElement.scrollTop = 0) || (document.body.scrollTop = 0)
      }
    }
  }
}
</script>
<style lang="postcss" scoped>
.back{
    position: fixed;
    bottom: 10%;
    right: 5%;
    width: 40px;
    height: 40px;
    z-index: 999;
    border-radius: 50%;
    background-color: yellow;
    color: #333;
    display: flex;
    justify-content: center;
}
.back-icon{
    align-self: center;
}
</style>
